{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Maximum Product Difference Between Two Pairs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maxProductDifference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #两个数对之间的最大乘积差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>两个数对 <code>(a, b)</code> 和 <code>(c, d)</code> 之间的 <strong>乘积差</strong> 定义为 <code>(a * b) - (c * d)</code> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，<code>(5, 6)</code> 和 <code>(2, 7)</code> 之间的乘积差是 <code>(5 * 6) - (2 * 7) = 16</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你一个整数数组 <code>nums</code> ，选出四个 <strong>不同的</strong> 下标 <code>w</code>、<code>x</code>、<code>y</code> 和 <code>z</code> ，使数对 <code>(nums[w], nums[x])</code> 和 <code>(nums[y], nums[z])</code> 之间的 <strong>乘积差</strong> 取到 <strong>最大值</strong> 。</p>\n",
    "\n",
    "<p>返回以这种方式取得的乘积差中的 <strong>最大值</strong> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [5,6,2,7,4]\n",
    "<strong>输出：</strong>34\n",
    "<strong>解释：</strong>可以选出下标为 1 和 3 的元素构成第一个数对 (6, 7) 以及下标 2 和 4 构成第二个数对 (2, 4)\n",
    "乘积差是 (6 * 7) - (2 * 4) = 34\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [4,2,5,9,7,4,8]\n",
    "<strong>输出：</strong>64\n",
    "<strong>解释：</strong>可以选出下标为 3 和 6 的元素构成第一个数对 (9, 8) 以及下标 1 和 5 构成第二个数对 (2, 4)\n",
    "乘积差是 (9 * 8) - (2 * 4) = 64\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>4 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [maximum-product-difference-between-two-pairs](https://leetcode.cn/problems/maximum-product-difference-between-two-pairs/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [maximum-product-difference-between-two-pairs](https://leetcode.cn/problems/maximum-product-difference-between-two-pairs/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[5,6,2,7,4]', '[4,2,5,9,7,4,8]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2]-nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()  # 将数组按升序排序\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]  # 返回最大乘积差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        return (nums[0] * nums[1]) - (nums[-1] * nums[-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        #找到两个最大的，找到两个最小的\n",
    "        num_max, num_min = 1,1\n",
    "        num_max *= max(nums)\n",
    "        nums.remove(max(nums))\n",
    "        num_max *= max(nums)\n",
    "\n",
    "        num_min *= min(nums)\n",
    "        nums.remove(min(nums))\n",
    "        num_min *= min(nums)\n",
    "\n",
    "        return num_max-num_min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2] - nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1] * nums[-2]) - (nums[0] * nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        return nums[0]*nums[1] - nums[-1]*nums[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "\n",
    "        max1=max(nums)\n",
    "        nums.remove(max1)\n",
    "        max2=max(nums)\n",
    "        min1=min(nums)\n",
    "        nums.remove(min1)\n",
    "        min2=min(nums)\n",
    "        return max1*max2-min1*min2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        if len(nums) < 4:\n",
    "            return 0\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[1] * nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1] * nums[-2]) - (nums[0] * nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort(key=lambda x: -x)\n",
    "        return nums[0] * nums[1] - nums[-2] * nums[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2] - nums[0]*nums[1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "\n",
    "        \n",
    "        return nums[-1]*nums[-2]-nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        a = nums[-1]\n",
    "        b = nums[-2]\n",
    "        c = nums[0]\n",
    "        d = nums[1]\n",
    "        return a*b-c*d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        numsSize = len(nums)\n",
    "        nums = list(sorted(nums))\n",
    "        return nums[numsSize - 1] * nums[numsSize - 2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2] - nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        max1, max2 = 0, 0\n",
    "        min1, min2 = 10001, 10001\n",
    "        for n in nums:\n",
    "            if n > max1:\n",
    "                max2 = max1\n",
    "                max1 = n\n",
    "            elif n > max2:\n",
    "                max2 = n\n",
    "\n",
    "            if n < min1:\n",
    "                min2 = min1\n",
    "                min1 = n\n",
    "            elif n < min2:\n",
    "                min2 = n\n",
    "        return max1 * max2 - min1 * min2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        maxNum1=max(nums)\n",
    "        minNum1=min(nums)\n",
    "        nums.remove(maxNum1)\n",
    "        nums.remove(minNum1)\n",
    "        maxNum2=max(nums)\n",
    "        minNum2=min(nums)\n",
    "        return maxNum1*maxNum2-minNum1*minNum2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2] - nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[len(nums)-1]*nums[len(nums)-2]-nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2]-nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n=len(nums)\n",
    "        return nums[n-1]*nums[n-2]-nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        length = len(nums)\n",
    "        nums.sort(reverse = False)\n",
    "        max1 = nums[length-1]\n",
    "        max2 = nums[length-2]\n",
    "        min1 = nums[0]\n",
    "        min2 = nums[1]\n",
    "        return max1*max2 - min1*min2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        print(nums.sort())\n",
    "        res=nums[-1]*nums[-2]-nums[0]*nums[1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[len(nums) - 1]*nums[len(nums) - 2] - nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1] * nums[-2]) - (nums[0] * nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\r\n",
    "        nums.sort()\r\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[- 1]*nums[- 2] - nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        min1 = 10001\n",
    "        min2= 0\n",
    "        max1 = 0\n",
    "        max2 = 0\n",
    "        \n",
    "        for i in nums:\n",
    "            if i < min1:\n",
    "                min2 = min1\n",
    "                min1 = i                \n",
    "            elif i < min2:\n",
    "                min2 = i\n",
    "            if i > max1:\n",
    "                max2 = max1\n",
    "                max1 = i\n",
    "            elif i > max2:\n",
    "                max2 = i\n",
    "        # print(min1,min2,max1,max2)\n",
    "        return (max1 * max2) - (min1 * min2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int: \n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2] - nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1]*nums[-2])-(nums[0]*nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        sort_arr = sorted(nums)\n",
    "        max_v = sort_arr[-1] * sort_arr[-2]\n",
    "        min_v = sort_arr[0] * sort_arr[1]\n",
    "        return max_v - min_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] *nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1]*nums[-2])-(nums[0]*nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums=sorted(nums);\n",
    "        return nums[-1]*nums[-2]-nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        l = len(nums)\n",
    "        return (nums[l-1] * nums[l-2]) - (nums[0] * nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def maxProductDifference(self, nums):\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2]-nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        return nums[-1]*nums[-2]-nums[1] * nums[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)-1\n",
    "        return nums[n]*nums[n-1]-nums[0]*nums[1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1] * nums[-2] - nums[0] * nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        s1, s2, l2, l1 = sorted(nums[:4])  # 最小、次小、次大、最大\n",
    "        for x in range(4, len(nums)):\n",
    "            if nums[x] > l1: l1, l2 = nums[x], l1\n",
    "            elif nums[x] > l2: l2 = nums[x]\n",
    "            elif nums[x] < s1: s1, s2 = nums[x], s1\n",
    "            elif nums[x] < s2: s2 = nums[x]\n",
    "        return l2 * l1 - s1 * s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        a, b, c, d = 1, 0, 1, 0\n",
    "        if nums[a] < nums[b]:\n",
    "            a, b = b, a\n",
    "        if nums[c] > nums[d]:\n",
    "            c, d = d, c\n",
    "        for i in range(2, len(nums)):\n",
    "            if nums[i] > nums[c] and nums[i] > nums[d]:\n",
    "                c, d = d, i\n",
    "            elif nums[i] > nums[c]:\n",
    "                c = i\n",
    "            if nums[i] < nums[a] and nums[i] < nums[b]:\n",
    "                a, b = b, i\n",
    "            elif nums[i] < nums[a]:\n",
    "                a = i\n",
    "        return nums[c] * nums[d] - nums[a] * nums[b]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1] * nums[-2])  - (nums[0] * nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=False)\n",
    "        a = nums[-1]  *  nums[-2]\n",
    "        b = nums[0]  *  nums[1]\n",
    "        return a - b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        mx1,mx2 = max(nums[0],nums[1]), min(nums[0],nums[1])\n",
    "        mn1,mn2 = min(nums[0],nums[1]), max(nums[0],nums[1])\n",
    "        for i in range(2,n):\n",
    "            tmp = nums[i]\n",
    "            if tmp>mx1:\n",
    "                mx1,mx2 = tmp,mx1\n",
    "            elif tmp>mx2:\n",
    "                mx2=tmp\n",
    "            if tmp<mn1:\n",
    "                mn1,mn2 = tmp,mn1\n",
    "            elif tmp<mn2:\n",
    "                mn2=tmp\n",
    "        return (mx1*mx2)-(mn1*mn2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1] * nums[-2] - nums[0] * nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        a1=a2=0\n",
    "        b1=b2=0\n",
    "        a1=max(nums)\n",
    "        b1=min(nums)\n",
    "        print(a1,b1)\n",
    "        nums.remove(a1)\n",
    "        nums.remove(b1)\n",
    "        print(nums)\n",
    "        a2=max(nums)\n",
    "        b2=min(nums)\n",
    "        return (a1*a2)-(b1*b2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nt=sorted(nums)\n",
    "        return (nt[-1]*nt[-2]) - (nt[0]*nt[1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        # nums.sort()\n",
    "        # return nums[-1]*nums[-2] - nums[0]*nums[1]\n",
    "\n",
    "\n",
    "        n = len(nums)\n",
    "        # 数组中最大的两个值\n",
    "        mx1, mx2 = max(nums[0], nums[1]), min(nums[0], nums[1])\n",
    "        # 数组中最小的两个值\n",
    "        mn1, mn2 = min(nums[0], nums[1]), max(nums[0], nums[1])\n",
    "        for i in range(2, n):\n",
    "            tmp = nums[i]\n",
    "            if tmp > mx1:\n",
    "                mx1, mx2 = tmp, mx1\n",
    "            elif tmp > mx2:\n",
    "                mx2 = tmp\n",
    "            if tmp < mn1:\n",
    "                mn1, mn2 = tmp, mn1\n",
    "            elif tmp < mn2:\n",
    "                mn2 = tmp\n",
    "        return (mx1 * mx2) - (mn1 * mn2)\n",
    "\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/maximum-product-difference-between-two-pairs/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1] * nums[-2] - nums[0] * nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        a = max(nums)\n",
    "        nums.remove(a)\n",
    "        b = max(nums)\n",
    "        nums.remove(b)\n",
    "        c = min(nums)\n",
    "        nums.remove(c)\n",
    "        d = min(nums)\n",
    "        return a*b - c*d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        max1, max2 = -1, -1\n",
    "        min1, min2 = 1e9, 1e9\n",
    "        for num in nums:\n",
    "            if num > max1:\n",
    "                max2 = max1\n",
    "                max1 = num\n",
    "            elif num > max2:\n",
    "                max2 = num\n",
    "            if num < min1:\n",
    "                min2 = min1\n",
    "                min1 = num\n",
    "            elif num < min2:\n",
    "                min2 = num\n",
    "        return max1 * max2 - min1 * min2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2]-nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return (nums[-1]*nums[-2])-(nums[0]*nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1]*nums[-2]-nums[0]*nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        return (nums[-1] * nums[-2]) - (nums[0] * nums[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        da=[max(nums),0]\n",
    "        xiao=[min(nums),10000]\n",
    "        nums.remove(max(nums))\n",
    "        nums.remove(min(nums))\n",
    "        for i in nums:\n",
    "            if i>da[1]:\n",
    "                da[1]=i\n",
    "            if i<xiao[1]:\n",
    "                xiao[1]=i\n",
    "        return (da[0]*da[1]-xiao[0]*xiao[1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        a=max(nums)\n",
    "        b=min(nums)\n",
    "        nums.remove(a)\n",
    "        nums.remove(b)\n",
    "        c=max(nums)\n",
    "        d=min(nums)\n",
    "        return a*c-b*d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return nums[-1] * nums[-2] - nums[0] * nums[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = (nums[-1] * nums[-2]) - (nums[0] * nums[1])\n",
    "        return res \n",
    "'''\n",
    "\n",
    "class Solution:\n",
    "    def maxProductDifference(self, nums: List[int]) -> int:\n",
    "        a = max(nums)\n",
    "        nums.remove(a)\n",
    "        b = max(nums)\n",
    "        nums.remove(b)\n",
    "        c = min(nums)\n",
    "        nums.remove(c)\n",
    "        d = min(nums)\n",
    "\n",
    "        return (a*b) - (c*d)\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
